package com.dianrui.fmi.util;

public class StringUtils {
    /**
     * 判断字符串是否为null或长度为0
     * s == null ||"null".equals(s)||"NULL".equals(s)|| s.length() == 0
     *
     * @param s 待校验字符串
     * @return {@code true}: 空<br> {@code false}: 不为空
     */
    public static boolean isEmpty(CharSequence s) {
        if (s == null){
            return true;
        }else {
            if (s.length() == 0){
                return true;
            }else {
                if ("".equals(s)||"null".equals(s)||"NULL".equals(s)){
                    return true;
                }
            }
        }
        return false;
    }

    public static float toFloat(Object obj){
        if (obj==null){
            return 0f;
        }
        return toFloat(obj.toString(),0f);
    }

    public static float toFloats(Object obj){
        if (obj==null){
            return 0.0000f;
        }
        return toFloat(obj.toString(),0.0000f);
    }

    /**
     * 对象转整
     *
     * @param obj
     * @return 转换异常返回 0
     */
    public static int toInt(Object obj) {
        if (obj == null) {
            return 0;
        }
        return toInt(obj.toString(), 0);
    }


    /**
     * 字符串转整数
     *
     * @param str
     * @param defValue
     * @return
     */
    public static int toInt(String str, int defValue) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
        }
        return defValue;
    }


    public static float toFloat(String str, float defValue){
        try {
            return Float.parseFloat(str);
        } catch (Exception e) {
        }
        return defValue;
    }
    /**
     * String转double
     *
     * @param obj
     * @return 转换异常返回 0
     */
    public static double toDouble(String obj) {
        try {
            if(StringUtils.isEmpty(obj)){
                return 0D;
            }
            return Double.parseDouble(obj);
        } catch (Exception e) {
        }
        return 0D;
    }
}
